//package com.bl.weilan.task.job;
//
//import com.bl.weilan.common.utils.DateUtils;
//import com.bl.weilan.dao.model.custom.ArenaSummaryEntity;
//import com.bl.weilan.service.biz.arena.ArenaScheduleService;
//import com.bl.weilan.service.biz.arena.ArenaService;
//import lombok.extern.log4j.Log4j2;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageRequest;
//import org.springframework.data.domain.Pageable;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//import java.util.List;
//
//
///**
// * Created by liangweikang on 2018/3/16.
// */
//@Log4j2
//@Component
//@EnableScheduling
//public class ArenaScheduleJob {
//
//    final static private int ARENA_SETTLE_PAGE_SIZE = 100;
//
//    @Autowired
//    ArenaService arenaService;
//
//    @Autowired
//    ArenaScheduleService arenaScheduleService;
//
//    @Autowired
//    ArenaDao arenaDao;
//
//    @Autowired
//    ArenaSummaryRepository arenaSummaryRepository;
//
//    @Autowired
//    ArenaRepository arenaRepository;
//
//    public void execute() {
//
//        log.info("schedule arena at " + DateUtils.formatDate(new Date(System.currentTimeMillis()), DateUtils.FORMAT_YYYY_MM_DD_HH_MM_SS));
//        long now = System.currentTimeMillis();
//
//        for (ArenaType arenaType : ArenaType.values()) {
//            long arenaEndDate = arenaDao.getCurArenaEndTime(arenaType);
//            if (arenaEndDate <= now) {
//                long startDate = arenaDao.getCurArenaStartTime(arenaType);
//                long duration = arenaDao.getArenaDuration(arenaType);
//                long nextEndDate = arenaEndDate + duration;
//                log.info("arena end. arena type: {}, start date: {}, end date: {}", arenaType.toString(), startDate, arenaEndDate);
//
//                // settle cur arena
//                long count = arenaRepository.countByStatusAndStartTimeAndType(OnlineStatus.ONLINE, startDate, arenaType);
//                log.info("begin to settle arena, count: {}", count);
//                for (int page = 0, rank = 1; rank <= count; ++page) {
//                    Pageable pageable = new PageRequest(page, ARENA_SETTLE_PAGE_SIZE);
//                    Page<ArenaSummaryEntity> arenaSummaries = arenaSummaryRepository.findByStatusAndStartTimeAndTypeOrderByCollect(
//                            DateUtils.formatDate(new Date(startDate), DateUtils.FORMAT_YYYY_MM_DD_HH_MM_SS), arenaType.toString(), OnlineStatus.ONLINE.toString(), pageable);
//                    List<ArenaSummaryEntity> arenaSummaryList = arenaSummaries.getContent();
//                    for (ArenaSummaryEntity arenaSummaryEntity : arenaSummaryList) {
//                        arenaScheduleService.finishArena(arenaSummaryEntity.getId(), rank);
//                        ++rank;
//                    }
//                }
//
//                // set new arena date
//                log.info("begin to set new arena date, start date: {}, end date: {}",
//                        DateUtils.formatDate(new Date(arenaEndDate), DateUtils.FORMAT_YYYY_MM_DD_HH_MM_SS),
//                        DateUtils.formatDate(new Date(nextEndDate), DateUtils.FORMAT_YYYY_MM_DD_HH_MM_SS));
//                arenaDao.setCurArenaStartTime(arenaEndDate, arenaType);
//                arenaDao.setCurArenaEndTime(nextEndDate, arenaType);
//
//                log.info("finish to settle arena, arena type: {}", arenaType.toString());
//            }
//        }
//
//    }
//
//
//}
